#include "ad_coursemgrlist.h"
#include "ui_ad_coursemgrlist.h"

#include <QMessageBox>

AD_CourseMgrList::AD_CourseMgrList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AD_CourseMgrList)
{
    ui->setupUi(this);

    model = new QSqlTableModel();
    model->setTable("courses");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    model->setHeaderData(0, Qt::Horizontal, tr("课程ID"));
    model->setHeaderData(1, Qt::Horizontal, tr("课程名"));
    model->setHeaderData(2, Qt::Horizontal, tr("课程学分"));

    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->verticalHeader()->setHidden(true);

    ui->tableView->setModel(model);

}

AD_CourseMgrList::~AD_CourseMgrList()
{
    delete ui;
}


void AD_CourseMgrList::on_pushButton_clicked()
{

    //获取表的行数
    int row = model->rowCount();

    //添加一行
    model->insertRow(row);

    model->setData(model->index(row, 0), model->data(model->index(row-1,0)).toInt()+1);
    model->setData(model->index(row, 1), tr("课程名"));
    model->setData(model->index(row, 2), tr("3"));

    //提交

}


void AD_CourseMgrList::on_pushButton_2_clicked()
{
    //获取选中的行
    int curRow = ui->tableView->currentIndex().row();

    //删除该行
    model->removeRow(curRow);

    model->select();
}

